﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using CarlosMisfortune.model;
using TileEngine;

namespace CarlosMisfortune.view
{
    public class PlayerView: GameObjectView
    {
        PlayerModel m_player;
        private Rectangle m_rect;

        public PlayerView(ContentManager a_content, model.PlayerModel a_player)
        {
            m_player = a_player;
            string name = "idle";
            m_animations.Add(name,
                new model.AnimationStrip(
                    a_content.Load<Texture2D>(@"textures\player\carlos_idle 01"),
                    300,//
                    550,//should read these values from xml...
                    1,//
                    name));
            m_animations[name].LoopAnimation = true;

            name = "walk";
            m_animations.Add(name,
                new model.AnimationStrip(
                    a_content.Load<Texture2D>(@"textures\player\carlos_walk2"),
                    a_content.Load<Dictionary<string, Rectangle>>(@"xml\player\carlos_walk2"),
                    name));            
            m_animations[name].LoopAnimation = true;

            m_player.FrameWidth = 300;
            m_player.FrameHeight = 550;

            m_rect = new Rectangle(15, 5, (int)((m_player.FrameWidth - 95) * m_player.Scale), (int)((m_player.FrameHeight - 35) * m_player.Scale));

            m_player.DrawDepth = 0.725f;
            m_player.Enabled = true;
            m_player.IsPlayer = true;

            PlayAnimation("idle");
        }

        public override void Update(GameTime a_gameTime, GameObject obj)
        {
            if (m_player.Alive)
            {
                string newAnimation = "idle";
                GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
                KeyboardState keyState = Keyboard.GetState();
                m_player.Velocity = new Vector2(0, m_player.Velocity.Y);
                if (keyState.IsKeyDown(Keys.Left) || (gamePad.ThumbSticks.Left.X < -0.3f))
                {
                    m_player.Flipped = true;
                    m_rect.X = 7; 

                    newAnimation = "walk";
                    m_player.Velocity = new Vector2(-m_player.MoveScale, m_player.Velocity.Y);
                }

                if (keyState.IsKeyDown(Keys.Right) || (gamePad.ThumbSticks.Left.X > 0.3f))
                {
                    m_player.Flipped = false;
                    m_rect.X = 15;

                    newAnimation = "walk";
                    m_player.Velocity = new Vector2(m_player.MoveScale, m_player.Velocity.Y);
                }

                if (keyState.IsKeyDown(Keys.Space) || (gamePad.Buttons.A == ButtonState.Pressed))
                {
                    if (m_player.OnGround)
                    {
                        m_player.Jump();
                        newAnimation = "idle";
                        //newAnimation = "jump";
                    }
                }

                //if (m_currentAnimation == "jump")
                //{
                //    newAnimation = "jump";
                //}

                if (newAnimation != m_currentAnimation)
                {
                    PlayAnimation(newAnimation);
                }
            }

            if (m_player.CollisionRectangle.X != m_rect.X)            
                m_player.CollisionRectangle = m_rect;

            m_player.Velocity += m_player.FallSpeed;
            base.Update(a_gameTime, m_player);
        }
    }
}
